<?php

namespace Analysis\Controller;

// include_once(dirname(__FILE__).'../../../vendor/autoload.php');
// use PHPExcel;
// use PHPExcel_IOFactory;


class DashController extends CommController
{
    public function index()
    {
        $static = D("Static");
        $pollsize = M("pollsize");
        $deallogs = M("deallogs");
        $salestaff = M("salestaff");

        $time = array("s" => strtotime(date("Y-m-d 00:00:00")), "e" => strtotime(date("Y-m-d", strtotime("+1 day"))));
        //query nums
        $this->assign("query", $pollsize->where("pol_time between " . $time['s'] . " and " . $time['e'])->count());
        //order data
        $con["dll_time"] = array("between", array($time['s'], $time['e']));
        $con["dll_status"] = 1;
        $this->assign("confirm", $deallogs->where($con)->count());
        $this->assign("confirm_gold", $deallogs->where($con)->sum("dll_amount"));
        $this->assign("list", $deallogs->where($con)->order("dll_status desc")->limit(5)->select());
        //member
        $this->assign("saler", $salestaff->where("ssa_time between " . $time['s'] . " and " . $time['e'])->count());
        //Chart Check
        $s = strtotime(date("Y-m-d"));
        $temp = date("Y-m-d", strtotime("-7 day"));
        $e = strtotime($temp);
        $chart = $static->getInitialParam($e, $s, "dll_time");
        $this->assign("chart_menu", json_encode($chart["menu"]));
        unset($chart["menu"]);
        $prints = $static->seleAndCountData($deallogs, $chart);
        $this->assign("chart_nums", json_encode($prints["nums"]));
        $this->assign("chart_gold", json_encode($prints["gold"]));
        // daily
        $this->assign("time", date("Y/m/d"));
        $this->display();
    }

    public function water($ods = 9)
    {
        $order = M("order", "yub_", C("CONN_YYB"));

        $this->assign("ods", $ods);
        $ods = $ods == "1" ? "1,2,3" : $ods;
        $ods = $ods == "4" ? "4,5" : $ods;
        $ods != 9 ? $where["ord_status"] = array("in", $ods) : null;
        $where["ord_discount"] = array("gt", 0);
        $data = commpage($order, $where, "ord_addtime desc", 12);
        $this->assign("data", $data);
        $this->display();
    }

    public function waterover($ors)
    {
        $couponhis = M("couponhis");
        $disisbn = M("disisbn");
        $good = M("orderblg", "yub_", C("CONN_YYB"));

        $info = $couponhis->where("cou_orders='" . $ors . "'")->find();
        $this->assign("info", $info);
        //Code
        $code = $disisbn->find($info['cou_disid']);
        $this->assign("code", $code);
        //get goods
        $list = $good->where("oeg_ordsn='" . $ors . "'")->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function salecode($cds = null)
    {
        $disisbn = M("disisbn");

        $cds != null ? $where["dis_custype"] = $cds : null;
        $where["dis_status"] = array("neq", 3);
        $data = commpage($disisbn, $where, "dis_time desc", 12);
        $this->assign("data", $data);
        $this->assign("sale", M("saletype")->where("sat_status=1")->select());
        $this->assign("cds", $cds);
        $this->display();
    }

    public function salecodeover($cod)
    {
        $disisbn = M("disisbn");
        $pollsize = M("pollsize");
        $cash = M("cash");

        $info = $disisbn->find($cod);
        $this->assign("info", $info);
        //get orderlist
        $list = $cash->where("c_disid=" . $cod)->select();
        $this->assign("all_count", count($list));
        $st = i_array_column($list, 'c_in');
        $this->assign("all_gold", array_sum($st));
        $listdata = commpage($cash, "c_disid=" . $cod, "c_time desc", 12);
        $this->assign("list", $listdata);
        //get chart
        $static = D("Static");
        $explo = $static->getInitialParam(strtotime("20" . $info["dis_timecode"]), strtotime("20" . $info["dis_endtimecode"]), "pol_time");
        $this->assign("menus", json_encode($explo["menu"]));
        unset($explo["menu"]);
        $prints = $static->pollAndCountData($pollsize, $explo, $info["dis_isbn"]);
        $this->assign("nums", json_encode($prints["nums"]));
        $this->display();
    }

    public function salecodeedit($cod)
    {
        $disisbn = M("disisbn");
        $preftype = M("preftype");
        $saletype = M("saletype");

        $data = $disisbn->find($cod);
        $this->assign("data", $data);
        //class
        $this->assign("pre", $preftype->where("pre_status=1")->select());
        $this->assign("sal", $saletype->where("sat_status=1")->select());
        $this->display();
    }

    public function proman()
    {
        $salestaff = M("salestaff");

        $data = commpage($salestaff, "ssa_status!=3", "ssa_time desc", 12);
        $this->assign("data", $data);
        $this->display();
    }

    public function promanover($soa)
    {
        $salestaff = M("salestaff");
        $disisbn = M("disisbn");
        $settlement = M("settlement");
        $cash=M("cash");

        $info = $salestaff->find($soa);
        $this->assign("info", $info);
        //get code
        $code = $disisbn->where("dis_ssaid=" . $soa)->select();
        $this->assign("code", $code);
        //paid his
        $paids = commpage($settlement, "set_ssaid=" . $soa, "set_time desc", 12);
        $this->assign("paid", $paids);
        // count data
        $this->assign("wait", $cash->where("c_status=1 and c_disid='" . $code[0]['dis_id']."'")->sum("c_in"));
        $this->assign("already", $settlement->where("set_status>=100 and set_status<300 and  set_ssaid=" . $soa)->sum("set_cash"));
        $this->assign("cash", $settlement->where("set_status=300 and  set_ssaid=" . $soa)->sum("set_cash"));
        $this->assign("paids", $settlement->where("set_status=400 and  set_ssaid=" . $soa)->sum("set_cash"));
        $this->display();
    }

    public function promanadd()
    {
        $salestaff = M("salestaff");
        $saletype = M("saletype");
        $this->assign("sal", $saletype->where("sat_status=1")->select());
        $this->display();
    }

    public function promanedit($soa)
    {
        $salestaff = M("salestaff");
        $saletype = M("saletype");

        $data = $salestaff->find($soa);
        $this->assign("data", $data);
        //class
        $this->assign("sal", $saletype->where("sat_status=1")->select());
        $this->display();
    }

    public function agent()
    {
        $saleagent = M("saleagent");

        $data = commpage($saleagent, "sag_status!=3", "sag_time desc", 12);
        $this->assign("data", $data);

        $this->display();
    }

    public function agentedit($sga)
    {
        $saleagent = M("saleagent");

        $this->assign("data", $saleagent->find($sga));
        $this->display();
    }

    public function agentover($sga)
    {
        $saleagent = M("saleagent");
        $salestaff = M("salestaff");
        $settlement = M("settlement");

        $info = $saleagent->find($sga);
        $this->assign("info", $info);
        //
        $data = commpage($salestaff, "ssa_agid=" . $sga, "ssa_time desc", 10);
        $this->assign("data", $data);
        //paid his
        $paids = commpage($settlement, "set_agent=" . $sga, "set_time desc", 10);
        $this->assign("paid", $paids);
        $this->display();
    }

    /**
     * 导出Excel
     * @author chenghuiyong <chenghuiyong@higgses.com>
     */
    public function exportXls()
    {
        vendor('PHPExcel.Classes.PHPExcel');

        $Excel = new \PHPExcel();

        // 设置
        $Excel
            ->getProperties()
            ->setCreator("chenghuiyong")
            ->setLastModifiedBy("chenghuiyong")
            ->setTitle("数据EXCEL导出")
            ->setSubject("数据EXCEL导出")
            ->setDescription("数据EXCEL导出")
            ->setKeywords("推广员列表")
            ->setCategory("推广员列表");

        $saleStaffs = M("salestaff")
            ->where('ssa_status != 3')
            ->order('ssa_time desc')
            ->select();

        $headers = [[
            'ssa_id' => 'ID',
            'ssa_name'=> '姓名',
            'ssa_phone'=> '账号',
            'ssa_banktrans'=> '开户银行',
            'ssa_banknumber'=> '银行账号',
            'ssa_type'=> '账户类型',
            'ssa_comp'=> '上级主管',
            'ssa_status'=> '状态',
            'ssa_time'=> '加入时间',
        ]];

        $results = array_merge($headers, $saleStaffs);
        // echo '<pre>',print_r($results);die;
        foreach ($results as $key => $val) { // 注意 key 是从 0 还是 1 开始，此处是 0
            $key++;
            switch ($val['ssa_status']) {
                case 1:
                    $sta = '启用';
                    break;
                case 2:
                    $sta = '启用';
                    break;
                case 8:
                    $sta = '审核';
                    break;
                default:
                    $sta = $val['ssa_status'];
                    break;
            }
            $Excel ->setActiveSheetIndex(0)
                 //Excel的第A列，uid是你查出数组的键值，下面以此类推
                  ->setCellValue('A'.$key, $val['ssa_id'])
                  ->setCellValue('B'.$key, $val['ssa_name'])
                  ->setCellValue('C'.$key, $val['ssa_phone'])
                  ->setCellValue('D'.$key, $val['ssa_banktrans'])
                  ->setCellValue('E'.$key, $val['ssa_banknumber'])
                  ->setCellValue('F'.$key, $key != 1 ? getDisName($val['ssa_type']) : $val['ssa_type'])
                  ->setCellValue('G'.$key, $val['ssa_comp'])
                  ->setCellValue('H'.$key, $sta)
                  ->setCellValue('I'.$key, $key != 1 ? date('Y-m-d H:i:s', $val['ssa_time']) : $val['ssa_time']);
        }

        $Excel->getActiveSheet()->setTitle('推广员列表');
        $Excel->setActiveSheetIndex(0);
        $name = time().'.xlsx';

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename='.$name);
        header('Cache-Control: max-age=0');

        $ExcelWriter = \PHPExcel_IOFactory::createWriter($Excel, 'Excel2007');
        $ExcelWriter->save('php://output');
        exit;
    }
}
